package cn.edu.ctbu.demo1.service.impl;

import cn.edu.ctbu.demo1.domain.Teacher;
import cn.edu.ctbu.demo1.domain.Teacher;
import cn.edu.ctbu.demo1.repository.TeacherRepository;
import cn.edu.ctbu.demo1.repository.TeacherRepository;
import cn.edu.ctbu.demo1.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherImplService implements TeacherService {
    @Autowired //帮我new一个对象 满足teacherRepository这个接口  IOC
    private TeacherRepository teacherRepository; //teacherRepository接口，不能new
    public List<Teacher> findAll(){
        return teacherRepository.findAll();
    }

    /**
     * 只支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Teacher> findAll(Pageable pageable){
        return teacherRepository.findAll(pageable);
    }
    public List<Teacher> findAll(Example<Teacher> example){
        return teacherRepository.findAll(example);
    }
    /**
     *按id进行查询
     * @param id 主键，整数
     * @return
     */
    public Teacher findById(Integer id){


        return  teacherRepository.findById(id).orElse(null);
    }

    /**
     *按姓名进行查询 ,like
     * @param name 主键，整数
     * @return
     */
    public List<Teacher> findByName(String name){

        return  teacherRepository.findByNameLike(name);
    }

    /**
     *按姓名和密码进行查询 ,like
     * @param name
     * @param password
     * @return
     */
    public List<Teacher> findByNameAndPassword(String name,String password){

        return  teacherRepository.findByNameAndPassword(name,password);
    }
    public List<Teacher> findByNoAndPassword(String no,String password){
        return teacherRepository.findByNoAndPassword(no, password);
    }

    public Teacher insert(Teacher teacher){
        teacherRepository.save(teacher);
        return teacher;
    }
    public Teacher update(Teacher teacher){
        teacherRepository.save(teacher);
        return teacher;
    }

    public void delete(Integer id){
//        Teacher teacher= new Teacher();
//        teacher.setId(id);
        teacherRepository.deleteById(id);
    }

    /**
     * 分页，带过滤的查询
     * @param teacher
     * @param pageable
     * @return
     */
    public Page<Teacher> findAll(Example<Teacher> teacher,Pageable pageable){
        return  teacherRepository.findAll(teacher,pageable);
    }
}
